import 'dart:collection';
import 'dart:convert';

import 'package:open_source_china/constants/constants.dart';
import 'package:open_source_china/model/user.dart';
import 'package:open_source_china/model/user_detial.dart';
import 'package:open_source_china/utils/DataUtils.dart';
import 'package:open_source_china/utils/NetUtils.dart';

class UserApi {
  /// 获取用户信息
  static Future<User> getInfo() async {
    String token = await DataUtils.getToken();
    User existUser = await DataUtils.getUserInfo();
    if (token == null || token?.length == 0) return null;
    // 本地存在用户信息即返回
    if (existUser != null) return existUser;
    Map<String, dynamic> params = Map();
    params[DataUtils.ACCESS_TOKEN] = token;
    var userStr = await NetUtils.post(AppUrls.USERINFO, params);
    Map userMap = json.decode(userStr);
    User user = User.fromJson(userMap);
    DataUtils.saveUserInfo(user); //保存用户信息到本地
    return user;
  }

  static Future<UserDetail> getUserDetail(int id) async {
    /// TODO:获取用户详情
    String token = await DataUtils.getToken();
    HashMap<String, dynamic> params = HashMap();
    params[DataUtils.ACCESS_TOKEN] = token;
    params["user"] = id;
    params["friend"] = id;
    var userStr = await NetUtils.get(AppUrls.USER_DETAIL, params);
    Map userMap = json.decode(userStr);
    UserDetail userDetail = UserDetail.fromJson(userMap);
    return userDetail;
  }
}
